<?php
declare (strict_types=1);

namespace app\common\model\customer;

use cores\BaseModel;

/**
 * 客户套餐服务模型
 * Class Service
 * @package app\common\model
 */
class Service extends BaseModel
{
    protected $name = 'customer_service';

    // 定义主键
    protected $pk = 'cs_id';
    protected $append = ['start_time_text', 'end_time_text', 'distance_days'];


    public function customer()
    {
        return $this->hasOne('Customer', 'customer_id', 'customer_id');
    }

    public function package()
    {
        return $this->hasOne('Package', 'package_id', 'package_id');
    }

    public function getStartTimeTextAttr($value, $data)
    {
        return !empty($data['start_time']) ? date("Y-m", $data['start_time']) : '';
    }

    public function getEndTimeTextAttr($value, $data)
    {
        return !empty($data['end_time']) ? date("Y-m", $data['end_time']) : '';
    }

    public function getDistanceDaysAttr($value, $data)
    {
        if (!$data['end_time']) {
            return '';
        }

        $datetime1 = new \DateTime();
        $datetime2 = new \DateTime(date('Y-m-d', $data['end_time']));
        $interval = $datetime1->diff($datetime2);
        return $interval->m;
    }
}
